package com.yf.anstudy;

import android.app.Activity;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

public class AndroidObj {

    private Activity activity;
    private WebView webView;

    public void setActivity(Activity act){
        this.activity = act;
    }

    public void setWebView(WebView webView){
        this.webView = webView;
    }
    // 定义JS需要调用的方法
    // 被JS调用的方法必须加入@JavascriptInterface注解
    @JavascriptInterface
    public void callAndroid(String msg){
        Log.e("zw","JS调用了Android的callAndroid()，msg : " + msg);
    }

    @JavascriptInterface
    public void test(String msg){
        Toast.makeText(activity.getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
        Log.e("zw","JS调用了Android的 test 方法，msg : " + msg);
        if(this.activity==null){
            Log.e("zw","activity is null");
            return;
        }
        this.activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webView.loadUrl("javascript:callJS()");
            }
        });

    }
}
